<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编号" prop="userName">
        <el-input
          v-model="queryParams.goodscode"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsname">
        <el-input
          v-model="queryParams.goodsname"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['flow:usergoods:add']"
        >添加绑定商品</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['flow:usergoods:remove']"
        >批量取消绑定</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['flow:usergoods:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="usergoodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="用户商品ID" align="center" prop="usergoodsid" />-->
      <!--      <el-table-column label="用户ID" align="center" prop="userId" />-->
      <el-table-column label="用户名称" align="center" key="userName" prop="userName"  />
      <!--      <el-table-column label="商品ID" align="center" prop="goodsid" />-->
      <el-table-column label="商品编号" align="center" prop="goodscode" />
      <el-table-column label="商品名称" align="center" prop="goodsname" />
      <el-table-column label="商品规格" align="center" prop="goodsspec" />
      <el-table-column label="生产厂家" align="center" prop="manufacturer" />
      <el-table-column label="商品产地" align="center" prop="place" />
      <el-table-column label="单位" align="center" prop="unit" />
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['flow:usergoods:edit']"-->
          <!--          >修改绑定</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['flow:usergoods:remove']"
          >取消绑定</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加用户商品关系对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <div style="display:flex;">
          <!--        <el-form-item label="商品ID" prop="goodsid">-->
          <!--          <el-input v-model="form.goodsid" placeholder="请输入商品ID" />-->
          <!--        </el-form-item>-->
        </div>
      </el-form>
      <!--   商品数据搜索   -->
      <el-form :model="queryParamsSp" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="商品编号" prop="goodscode">
          <el-input
            v-model="queryParamsSp.goodscode"
            placeholder="请输入商品编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsname">
          <el-input
            v-model="queryParamsSp.goodsname"
            placeholder="请输入商品名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="生产厂家" prop="manufacturer">
          <el-input
            v-model="queryParamsSp.manufacturer"
            placeholder="请输入生产厂家"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuerySp">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuerySp">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="goodsdocList" @selection-change="handleSelectionChangeSp">
        <el-table-column type="selection" width="55" align="center" />
        <!--      <el-table-column label="商品ID" align="center" prop="goodsid" />-->
        <el-table-column label="商品编号" align="center" prop="goodscode" />
        <el-table-column label="商品名称" align="center" prop="goodsname" />
        <el-table-column label="商品规格" align="center" prop="goodsspec" />
        <el-table-column label="生产厂家" align="center" prop="manufacturer" />
        <el-table-column label="商品产地" align="center" prop="place" />
        <el-table-column label="单位" align="center" prop="unit" />
        <!--        <el-table-column label="备注" align="center" prop="remark" />-->
        <!--        <el-table-column label="图片附件" align="center" prop="piclink" width="100">-->
        <!--          <template slot-scope="scope">-->
        <!--            <image-preview :src="scope.row.piclink" :width="50" :height="50"/>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
      </el-table>
      <!--  总条数-页码    -->
      <pagination
        v-show="totalSp>0"
        :total="totalSp"
        :page.sync="queryParamsSp.pageNum"
        :limit.sync="queryParamsSp.pageSize"
        @pagination="getListSp"
      />
      <!-- 提交 -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--  修改用户商品关系弹窗  -->
    <el-dialog :title="titleXg" :visible.sync="openXg" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户商品ID" prop="entid">
          <el-input v-model="form.usergoodsid" placeholder="请输入企业ID" />
        </el-form-item>
        <el-form-item label="用户ID" prop="ownercode">
          <el-input v-model="form.userId" placeholder="请输入货主编号" />
        </el-form-item>
        <el-form-item label="商品ID" prop="ownername">
          <el-input v-model="form.goodsid" placeholder="请输入货主名称" />
        </el-form-item>
        <el-form-item label="备注" prop="ownername">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormXg">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUsergoods, getUsergoods, delUsergoods, addUsergoods, updateUsergoods } from "@/api/flow/usergoods";
import {listGoodsdoc} from "@/api/flow/goodsdoc";

export default {
  name: "Usergoods",
  data() {
    return {
      //用户信息查询时的日期范围,填空即可
      dateRange:[],
      // 用户表格数据-用户id从此处调取
      userList: null,
      // 用户信息总条数
      totalYh: 0,
      // 商品资料表格数据
      goodsdocList: [],
      // 商品数据总条数
      totalSp: 0,
      // 选中数组
      idsSp: [],
      //商品选中数组-王捷
      idsSpXz: [],
      // 是否显示修改弹出层
      openXg: false,
      // 弹出层-修改的标题
      titleXg: "",
      // 商品的非单个禁用-修改相关
      singleSp: true,
      // 商品的非多个禁用-删除相关
      multipleSp: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //选中商品名数组
      idsSpMc: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户商品关系表格数据
      usergoodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId:null,
        userName: null,
        goodscode:null,
        goodsname: null,
      },
      // 商品数据查询参数
      queryParamsSp: {
        pageNum: 1,
        pageSize: 10,
        goodscode: null,
        goodsname: null,
        goodsspec: null,
        manufacturer: null,
        place: null,
        unit: null,
        piclink: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    let querys= this.$route.query
    if(querys){
      console.log('参数',querys)
      this.queryParams.userId=querys.id
    }
    this.getList();
  },
  methods: {
    /** 查询用户商品关系列表 */
    getList() {
      this.loading = true;
      console.log('查询数据',this.queryParams)
      listUsergoods(this.queryParams).then(response => {
        console.log('返回数据',response.rows)
        this.usergoodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openXg=false
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        usergoodsid: null,
        userId: null,
        goodsid: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log('选中数据',selection)
      this.ids = selection.map(item => item.usergoodsid)
      this.idsSpMc= selection.map(item => item.goodsname)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 商品数据多选框选中数据
    handleSelectionChangeSp(selection) {
      console.log('商品多选框1',selection)
      this.idsSpXz=selection
      this.idsSp = selection.map(item => item.usergoodsid)
      this.singleSp = selection.length!==1
      this.multipleSp = !selection.length

      console.log('商品多选框',this.idsSpXz)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户商品关系";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log('修改',row)
      this.reset();
      const usergoodsid = row.usergoodsid || this.idsSp
      getUsergoods(usergoodsid).then(response => {
        this.form = response.data;
        this.openXg = true;
        this.titleXg = "修改用户商品关系";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log('商品数据',this.form)
      for(let i=0;i<this.idsSpXz.length;i++){
        this.form.goodsid=this.idsSpXz[i].goodsid
        if(this.queryParamsSp.status){
          this.form.userId=this.queryParamsSp.status
        }
        if(this.queryParams.userId){
          this.form.userId=this.queryParams.userId
        }

        //提交绑定
        this.$refs["form"].validate(valid => {
          if (valid) {
            console.log('新增',this.form)
            addUsergoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        });
      }
    },
    //修改弹窗的提交按钮
    submitFormXg() {
      this.openXg = false;
      updateUsergoods(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.openXg = false;
        this.getList();
      });
    },
    // 关闭返回按钮
    handleClose() {
      const obj = { path: "/flow/flows/userBinding" };
      this.$tab.closeOpenPage(obj);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log('商品数据',row)
      const usergoodsids = row.goodsname || this.idsSpMc;
      this.$modal.confirm('是否确认删除用户商品"' + usergoodsids + '"的数据项？').then(function() {
        return delUsergoods(usergoodsids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('flow/usergoods/export', {
        ...this.queryParams
      }, `usergoods_${new Date().getTime()}.xlsx`)
    },
    /** 商品数据搜索按钮操作 */
    handleQuerySp() {
      this.queryParamsSp.pageNum = 1;
      this.getListSp();
    },
    /** 商品数据重置按钮操作 */
    resetQuerySp() {
      this.resetForm("queryForm");
      this.handleQuerySp();
    },
    /** 查询商品资料列表 */
    getListSp() {
      this.loading = true;
      listGoodsdoc(this.queryParamsSp).then(response => {
        this.goodsdocList = response.rows;
        this.totalSp = response.total;
        this.loading = false;
      });
    },
  }
};
</script>
